package com.example.wechat.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.annotation.Log;
import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.core.page.TableDataInfo;
import com.example.project.entity.User;
import com.example.project.enums.BusinessType;
import com.example.project.service.CommonService;
import com.example.project.utils.poi.ExcelUtil;
import com.example.wechat.model.WechatConversation;
import com.example.wechat.model.WechatUserContact;
import com.example.wechat.service.WechatConversationService;

/**
 * 聊天会话Controller
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@Controller
@RequestMapping("/wechat/conversation")
public class WechatConversationController extends BaseController {

	@Autowired
	private CommonService commonService;

	@Autowired
	private WechatConversationService wechatConversationService;

	/**
	 * 建立好友关系
	 */
	@GetMapping("/queryConversation")
	@ResponseBody
	public AjaxResult queryConversation(long targetId) {
		Integer sessionType = 1;
		long userId = getUserId();
		WechatConversation queryConversation = wechatConversationService.queryConversation(userId, targetId,
				sessionType);
		if (queryConversation == null) {
			User user = getUser();
			// 查询用户信息
			User contactUserInfo = commonService.getUserInfo(targetId);
			if (contactUserInfo == null) {
				return error(targetId + ":聊天对象不存在");
			}
			// 创建好友关系
			queryConversation = wechatConversationService.insert(user, contactUserInfo, sessionType);
			wechatConversationService.insert(contactUserInfo, user, sessionType);
		}
		return success(queryConversation);
	}
}
